package com.bruce.starts.demo.commonerror.service;

import com.bruce.starts.demo.commonerror.dao.UserRepository;
import com.bruce.starts.demo.commonerror.entity.UserEntity;
import com.bruce.starts.demo.commonerror.spring.Metrics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author luotuan
 * @Description
 * @create 2021-05-12 17:02
 **/
@Service
@Slf4j
public class UserService {
    @Autowired
    private UserRepository userRepository;

    //@Transactional
    //@Metrics
    public void createUser(UserEntity userEntity) {
        userRepository.save(userEntity);
        if (userEntity.getName().contains("test")) {
            throw new RuntimeException("invalid username");
        }
    }

    public int getUserCount(String name) {
        return userRepository.findByName(name).size();
    }
}
